$(function() {
	var type = $("#type").val();

	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/User/getDatas/"+type,
		paramFunc: paramFunc,
		operation: {
			"edit": ["Admin/User/edit/#id/"+type,"编辑"],
			"icon-trash": [deleteModule, "删除"],
		//	"icon-lock": [initPassword, "初始化密码"],
		//	"icon-refresh": [changeStatus, "启用/冻结"]
		},
		hasCheckbox: false,
		afterFunc:afterFunc
	});

	// exportEvent();
});

var afterFunc = function(){
	var type = $("#type").val();
	var num = 0;

	$("#table tbody tr").each(function() {
		var data = $("#table").getOpts().datas[$(this).index()];
	//	console.log(data);
		if(data.is_edit == 0){ //可删除,可编辑
			$(this).find('#fa-edit').show();
			$(this).find('#icon-trash').show();
		}else if(data.is_edit == 1){ //可编辑,不可删除
			$(this).find('#fa-edit').show();
			$(this).find('#icon-trash').hide();
		}else if(data.is_edit == 2){ //不可编辑.不可删除
			console.log(3);
			$(this).find('#fa-edit').hide();
			$(this).find('#icon-trash').hide();			
		}else if(data.is_edit == 3){//教师等于自己的时候
			num += 1;
			$(this).find('#fa-edit').show();
			$(this).find('#icon-trash').hide();
		}
	
	});

	if(num >0){
		console.log(5);
		$("#table tbody tr").each(function() {
			$("#butt").show();
			$(this).find('td:nth-child(2)').show();
		});
	}else{
		console.log(6);
		$("#table tbody tr").each(function() {
			var data = $("#table").getOpts().datas[$(this).index()];
			if(data.is_edit == 0){
				$("#butt").show();
				$(this).find('td:nth-child(2)').show();
			}else if(data.is_edit == 1){
				$("#butt").show();
				$(this).find('td:nth-child(2)').show();
			}else{
				$("#butt").hide();
				$(this).find('td:nth-child(2)').hide();	
			}
			
		});
	}
	
}

var paramFunc = function() {
	var data = getFormJson();
	return data;
};


var deleteModule=function(idx, trData){
	var msg = "是否删除?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/User/delete/"+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
}
